<?php
require_once('../converters.php');
require_once('../ResourceArray.class.php');

class Converter_TestCase extends PHPUnit_TestCase{

	var $object;
	var $namespaces = array(
	    'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
	    'sioc' => 'http://rdfs.org/sioc/ns#',
	    'dc' => 'http://purl.org/dc/elements/1.1/',
	    'terms' => 'http://purl.org/dc/terms/',
	    'foaf' => 'http://xmlns.com/foaf/0.1/',
	    'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
	    'rss' => 'http://purl.org/rss/1.0/',
	    'owl' => 'http://www.w3.org/2002/07/owl#',
	    'rel' => 'http://purl.org/vocab/relationship/',
	    'ibis' => 'http://dannyayers.com/2003/12/ibis.rdfs#',
	    'doap' => 'http://usefulinc.com/ns/doap#',
	    'frbr' => 'http://purl.org/vocab/frbr/core#',
		'xsd' => 'http://www.w3.org/2001/XMLSchema#',
	  );

	var $resources = array (
	    'http://example.org/test/JohnSmith' => 
	    array (
	      'http://xmlns.com/foaf/0.1/name' => 
	      array (
	        0 => 
	        array (
	          'value' => 'John Smith',
	          'type' => 'literal',
	        ),
	      ),
	      'http://xmlns.com/foaf/0.1/mbox' => 
	      array (
	        0 => 
	        array (
	          'value' => 'mailto:john@example.org',
	          'type' => 'uri',
	        ),
	      ),
	      'http://www.w3.org/2000/01/rdf-schema#label' => 
	      array (
	        0 => 
	        array (
	          'value' => 'Label',
	          'lang' => 'en-gb',
	          'type' => 'literal',
	        ),
	      ),
	    ),
	  );
	var $form_array_with_uris;
	
	var $resources_from_form;

	var $rdfxml = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:terms="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:rss="http://purl.org/rss/1.0/" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rel="http://purl.org/vocab/relationship/" xmlns:ibis="http://dannyayers.com/2003/12/ibis.rdfs#" xmlns:doap="http://usefulinc.com/ns/doap#" xmlns:frbr="http://purl.org/vocab/frbr/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" ><rdf:Description rdf:about="http://example.org/test/JohnSmith"><foaf:name>John Smith</foaf:name><foaf:mbox  rdf:resource="mailto:john@example.org"/><rdfs:label xml:lang="en-gb">Label</rdfs:label></rdf:Description></rdf:RDF>';
	
	var $sparql_xml = '<sparql
	    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	    xmlns:xs="http://www.w3.org/2001/XMLSchema#"
	    xmlns="http://www.w3.org/2005/sparql-results#" >
	  <head>
	    <variable name="s"/>
	    <variable name="p"/>
	    <variable name="o"/>
	  </head>
	  <results ordered="false" distinct="true">
	    <result>
	      <binding name="s">

	        <uri>http://api.talis.local/bf/stores/engagetenantstore/items/1173364330999#self</uri>
	      </binding>
	      <binding name="p">
	        <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#subject</uri>
	      </binding>
	      <binding name="o">
	        <uri>http://api.talis.local/bf/stores/engagetenantstore/items/1174262688178#self</uri>
	      </binding>
	    </result>
	    <result>
	      <binding name="s">
	        <uri>http://api.talis.local/bf/stores/engagetenantstore/items/1173364330999#self</uri>

	      </binding>
	      <binding name="p">
	        <uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#object</uri>
	      </binding>
	      <binding name="o">
	        <literal>1a3c47c9-fb29-4fd2-a061-a3b72328c96b</literal>
	      </binding>
	    </result>

	  </results>
	</sparql>';

	var $select_array = array(
	'head' => array(
		'vars' => array('s','p','o'),
		),
	'results' => array('bindings' => 
	array(
	  0 => 
	  array(
	    's' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://api.talis.local/bf/stores/engagetenantstore/items/1173364330999#self',
	    ),
	    'p' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject',
	    ),
	    'o' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://api.talis.local/bf/stores/engagetenantstore/items/1174262688178#self',
	    ),
	  ),
	  1 => 
	  array(
	    's' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://api.talis.local/bf/stores/engagetenantstore/items/1173364330999#self',
	    ),
	    'p' => 
	    array(
	      'type' => 'uri',
	      'value' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#object',
	    ),
	    'o' => 
	    array(
	      'type' => 'literal',
	      'value' => '1a3c47c9-fb29-4fd2-a061-a3b72328c96b',
	    ),
	  ),
	),
	),
	);
	
	var $sparql_true = '<sparql xmlns="http://www.w3.org/2005/sparql-results#">
		<head></head>
		<results>
			<boolean>TRUE</boolean>
		</results>
	</sparql>';
	
	var $sparql_false = '<sparql xmlns="http://www.w3.org/2005/sparql-results#">
		<head></head>
		<results>
			<boolean>FALSE</boolean>
		</results>
	</sparql>';

	var $turtle = '
		@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
		@prefix ns0: <http://xmlns.com/foaf/0.1/> .
		@prefix ns1: <http://www.w3.org/2000/01/rdf-schema#> .

		<http://example.org/test/JohnSmith> ns0:name "John Smith" ;
		                                    ns0:mbox <mailto:john@example.org> ;
		                                    ns1:label "Label"@en-gb .
			  ';
	
	function __construct($name)
	{
		$this->PHPUnit_TestCase($name);
		$this->form_array_with_uris = array(
				'prefixes' => $this->namespaces,
				'resources' => array(
					'http://example.org/test/JohnSmith' => array(
						'foaf:name' => 'John Smith',
		    			'rdfs:label@en-gb' => 'Label',
						'foaf:mbox' => 'mailto:john@example.org',
						'foaf:nick' => array('johnno','johnsmith'),
						'foaf:age^^xsd:integer' => 21,
						),
					'foaf:Person' => array(
						'foaf:name' => 'John Smith',
						'rdfs:label@en-gb' => 'Label',
						'foaf:mbox' => 'mailto:john@example.org',
						'foaf:nick' => array('johnno','johnsmith'),
						'foaf:age^^xsd:integer' => 21,
						),
					),
			);

		$properties = array_shift(array_values($this->resources));
		$bnode_resources = array('_:bnode1' => $properties);
	   	$this->resources_from_form = array_merge_recursive($this->resources);
	}
	
	function setUp(){
		$this->object = new Converter($this->namespaces, '../../arc2/ARC2.php');
	}
	
	function tearDown(){
		unset($this->object);
	}
	
	
	function test_RDFXML_to_resources() {
		$ex = $this->resources;
		$act = $this->object->RDFXML->to_resources($this->rdfxml)->value();
		$this->assertEquals($ex, $act);		
	}
	function test_Resources_to_rdfxml() {
		$rdf = <<<_RDF_
			<rdf:RDF
			    xmlns:foaf="http://xmlns.com/foaf/0.1/"
			    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
			    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
			    xmlns:j.0="http://schemas.talis.com/2005/dir/schema#"
			    xmlns:j.1="http://xmlns.com/wot/0.1/"
			    xmlns:dc="http://purl.org/dc/elements/1.1/" > 
			  <rdf:Description rdf:about="http://eikeon.com/foaf.rdf">
			    <rdfs:seeAlso>http://donnafales.com/foaf.rdf</rdfs:seeAlso>
			    <rdf:type>http://xmlns.com/foaf/0.1/Document</rdf:type>
			    <rdfs:seeAlso>http://heddley.com/edd/foaf.rdf</rdfs:seeAlso>
			    <rdfs:label>eikeon's FOAF document</rdfs:label>
			    <rdfs:seeAlso>http://xmlns.com/foaf/0.1/</rdfs:seeAlso>

			    <j.1:assurance>http://eikeon.com/foaf.rdf.asc</j.1:assurance>
			    <rdfs:comment>Document with Friend-of-a-Friend description of eikeon.

			edited</rdfs:comment>
			    <j.0:etag>6c35f4ed-db6f-48fe-b871-a41ae3165ead</j.0:etag>
			    <rdfs:seeAlso>http://www.perceive.net/xml/foaf.rdf</rdfs:seeAlso>
			    <rdfs:seeAlso>http://iama.rrecktek.com/~rreck/foaf.rdf</rdfs:seeAlso>
			  </rdf:Description>

			</rdf:RDF>
_RDF_;
		$ex = $this->object->RDFXML->to_triples($rdf)->value();
		$act = $this->object->RDFXML->to_triples($this->object->RDFXML->to_resources($rdf)->to_rdfxml())->value();
		$this->assertEquals(count($ex), count($act));		
	}
	
	function test_Resources_to_turtle() {
		$act = preg_replace('/\s/','',$this->object->RDFXML->to_resources($this->rdfxml)->to_turtle());
		$ex = preg_replace('/\s/','',$this->turtle);
		$this->assertEquals($ex, $act);		
	}
	
	function test_sparqlxml_to_array()
	{
		$ex = $this->select_array;
		$act = $this->object->SPARQL_XML->to_array($this->sparql_xml)->value();
		$this->assertEquals($ex, $act);		
	}

	function test_sparqlxml_to_array_boolean_true()
	{
		$ex = array(
			'head'=> 
			array(
				),
			'boolean' => true,
		);
		$act = $this->object->SPARQL_XML->to_array($this->sparql_true)->value();
		$this->assertEquals($ex, $act);		

	}

	function test_sparqlxml_to_array_boolean_false()
	{
		$ex = array(
			'head'=> array(
					),
			'boolean' => false,
		);
		$act = $this->object->SPARQL_XML->to_array($this->sparql_false)->value();
		$this->assertEquals($ex, $act);		
	}
	
	
	function test_sparqlxml_to_boolean_true()
	{
		$ex = 'true';
		$act = $this->object->SPARQL_XML->to_boolean($this->sparql_true);
		$this->assertEquals($ex, $act);		
	}
	
	function test_sparqlxml_to_boolean_false()
	{
		$ex = 'false';
		$act = $this->object->SPARQL_XML->to_boolean($this->sparql_false);
		$this->assertEquals($ex, $act);		
	}
	
	function test_Qname_static_to_term()
	{
		$ex = 'name';
		$act = Qname::to_term('foaf:name');
		$this->assertEquals($ex,$act);		
	}
	
	function test_Qname_to_term()
	{
		$ex = 'name';
		$act = $this->object->Qname->to_term('foaf:name');
		$this->assertEquals($ex,$act);
	}
	
	function test_Qname_static_to_uri()
	{
		$ex = new URI('http://xmlns.com/foaf/0.1/name');
		$act = Qname::to_uri('foaf:name', $this->namespaces);
		$this->assertTrue($act->value()===$ex->value());
	}
	
	function test_Qname_to_uri()
	{
		$ex = new URI('http://xmlns.com/foaf/0.1/name');
		$act = $this->object->Qname->to_uri('foaf:name');
		$this->assertTrue($act->value()===$ex->value());
	}
	
	function test_Qname_static_to_eRDF_term()
	{
		$ex = 'foaf-Person';
		$act = Qname::to_eRDF_term('foaf:Person');
		$this->assertEquals($ex,$act);	}
	
	function test_Qname_to_eRDF_term()
	{
		$ex = 'foaf-Person';
		$qname = new Qname('foaf:Person');
		$act = $qname->to_eRDF_term();
		$this->assertEquals($ex,$act);	
	}
	
	function test_URI_to_eRDF_term()
	{
		$act= $this->object->URI->to_eRDF_term('http://xmlns.com/foaf/0.1/name');
		$ex = 'foaf-name';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_to_namespace()
	{
		$act= $this->object->URI->to_namespace('http://xmlns.com/foaf/0.1#name');
		$ex = 'http://xmlns.com/foaf/0.1#';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_to_namespace_with_dc()
	{
		$act= $this->object->URI->to_namespace('http://purl.org/dc/elements/1.1/title');
		$ex = 'http://purl.org/dc/elements/1.1/';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_to_qname_with_dc()
	{
		$act= $this->object->URI->to_qname('http://purl.org/dc/elements/1.1/title')->value();
		$act= $this->object->URI->to_qname('http://purl.org/dc/elements/1.1/title')->value();
		$ex = 'dc:title';
		$this->assertEquals($ex,$act);	
	}

	
	function test_URI_to_qname()
	{
		$act= $this->object->URI->to_qname('http://xmlns.com/foaf/0.1/name')->value();
		$ex = 'foaf:name';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_to_term()
	{
		$act= $this->object->URI->to_term('http://xmlns.com/foaf/0.1/name');
		$ex = 'name';
		$this->assertEquals($ex,$act);	
	}
	function test_URI_split()
	{
		$act= $this->object->URI->split('http://xmlns.com/foaf/0.1/name');
		$ex = array('http://xmlns.com/foaf/0.1/','name');
		$this->assertEquals($ex,$act);	
	}

	function test_URI_to_label()
	{
			$in = 'http://example.com/foo/bar#CamelCaseEverything_term_test';
			$e =  'Camel Case Everything Term Test';
			$a = $this->object->URI->to_label($in);
			$this->assertEquals($e, $a);
	}

	
	function test_Resource_get_title(){
		$Resource = new ResourceArray($this->resources);
		$ex = 'John Smith';
		$act = $Resource->get_title();
		$nothing = $Resource->property('http://xmlns.com/foaf/0.1/name');
		$this->assertTrue(($ex==$act) && $nothing===false);	
		
	}
	
	function test_Resources_get_languages(){
		$ex = array('en-gb');
		$act = $this->object->GraphIndex->get_languages($this->resources);
		$this->assertEquals($ex,$act);	
	}
	
	function test_Resources_to_resource()
	{
		$r = $this->resources;
		$resources = array(array_shift(array_keys($r)) => array_shift(array_values($r)));
		$ex = new ResourceArray($resources, $this->namespaces);
		$Resources = new GraphIndex($this->resources, $this->namespaces);
		$act = $Resources->to_resource(array_shift(array_keys($r)));
		$this->assertEquals($ex->value(),$act->value());	
	}
	
	function test_Resource_property(){
		$Resources = new GraphIndex($this->resources, $this->namespaces);
		$r = $this->resources;
		$Res = $Resources->to_resource(array_shift(array_keys($r)));
		$act = $Res->property('http://xmlns.com/foaf/0.1/name');
		$ex = $this->resources['http://example.org/test/JohnSmith']['http://xmlns.com/foaf/0.1/name'][0]['value'];
		$this->assertEquals($ex,$act);	
		
	}
	
	function test_Resource_property_unset(){
		$Resources = new GraphIndex($this->resources, $this->namespaces);
		$r = $this->resources;
		
		$Res = $Resources->to_resource(array_shift(array_keys($r)));
		$name = $Res->property('http://xmlns.com/foaf/0.1/name');
		$act = $Res->property('http://xmlns.com/foaf/0.1/name');
		$ex = false;
		$this->assertEquals($ex,$act);	
		
	}
	
	function test_Resources_filter_by_language_en_gb(){
		$Resources = new GraphIndex($this->resources);
		$ex = $this->resources;
		$act = $Resources->filter_by_language('en')->value();
		$this->assertEquals($ex,$act);		
	}
	
	function test_resourcesArray_is_valid_true(){
		
		$expected = true;
		$actual = $this->object->GraphIndex->is_valid($this->resources);
		$this->assertTrue( $actual);
	}
	
	function test_resourcesArray_is_valid_false_too_shallow(){
		
		$expected = false;
		$false_rdfjson = array('#foo' => array('#no'=> array(1,2,3,4)));
		$actual = $this->object->GraphIndex->is_valid($false_rdfjson);
		$this->assertFalse($actual);
	}

	function test_resourcesArray_is_valid_false_too_deep(){
		
		$expected = false;
		$false_rdfjson = array('#foo' => array('#no'=> array(array('value'=>array(1,2,3,4)))));
		$actual = $this->object->GraphIndex->is_valid($false_rdfjson);
		$this->assertFalse( $actual);
	}
	
	function test_resourcesArray_is_valid_false_not_array(){
		
		$false_rdfjson = array('#foo' => array('#no'=> array(array('value'=>array(1,2,3,4)))));
		$actual = $this->object->GraphIndex->is_valid("hello world");
		$this->assertFalse( $actual);
	}
	
	function test_formsArray_to_resourcesArray(){
		$ex = array(
		  'http://example.org/test/JohnSmith' => 
		  array(
		    'http://xmlns.com/foaf/0.1/name' => 
		    array(
		      0 => 
		      array(
		        'value' => 'John Smith',
		        'type' => 'literal',
		      ),
		    ),
		    'http://www.w3.org/2000/01/rdf-schema#label' => 
		    array(
		      0 => 
		      array(
		        'value' => 'Label',
		        'type' => 'literal',
		        'lang' => 'en-gb',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/mbox' => 
		    array(
		      0 => 
		      array(
		        'value' => 'mailto:john@example.org',
		        'type' => 'literal',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/nick' => 
		    array(
		      0 => 
		      array(
		        'value' => 'johnno',
		        'type' => 'literal',
		      ),
		      1 => 
		      array(
		        'value' => 'johnsmith',
		        'type' => 'literal',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/age' => 
		    array(
		      0 => 
		      array(
		        'value' => 21,
		        'type' => 'literal',
		        'datatype' => 'xsd:integer',
		      ),
		    ),
		  ),
		  '_:bnode1' => 
		  array(
		    'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 
		    array(
		      0 => 
		      array(
		        'type' => 'uri',
		        'value' => 'http://xmlns.com/foaf/0.1/Person',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/name' => 
		    array(
		      0 => 
		      array(
		        'value' => 'John Smith',
		        'type' => 'literal',
		      ),
		    ),
		    'http://www.w3.org/2000/01/rdf-schema#label' => 
		    array(
		      0 => 
		      array(
		        'value' => 'Label',
		        'type' => 'literal',
		        'lang' => 'en-gb',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/mbox' => 
		    array(
		      0 => 
		      array(
		        'value' => 'mailto:john@example.org',
		        'type' => 'literal',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/nick' => 
		    array(
		      0 => 
		      array(
		        'value' => 'johnno',
		        'type' => 'literal',
		      ),
		      1 => 
		      array(
		        'value' => 'johnsmith',
		        'type' => 'literal',
		      ),
		    ),
		    'http://xmlns.com/foaf/0.1/age' => 
		    array(
		      0 => 
		      array(
		        'value' => 21,
		        'type' => 'literal',
		        'datatype' => 'xsd:integer',
		      ),
		    ),
		  ),
		);
		$act = $this->object->FormArray->to_resources($this->form_array_with_uris)->value();
		$this->assertEquals($ex,$act);	
	}

	function test_formsArray_parse_property_lang(){
		$ex = array('lang'=>'en-gb','datatype'=>false,'property'=>'rdfs:label');
		$act = $this->object->FormArray->parse_property('rdfs:label@en-gb');
		$this->assertEquals($ex,$act);	
	}
	function test_formsArray_parse_property_datatype(){
		$ex = array('lang'=>false,'datatype'=>'xsd:string','property'=>'rdfs:label');
		$act = $this->object->FormArray->parse_property('rdfs:label^^xsd:string');
		$this->assertEquals($ex,$act);	
	}	
	
	function test_formsArray_parse_property(){
		$ex = array('lang'=>false,'datatype'=>false,'property'=>'rdfs:label');
		$act = $this->object->FormArray->parse_property('rdfs:label');
		$this->assertEquals($ex,$act);	
	}
	
	function test_GraphIndex_to_rdfxml_with_bnodes()
	{
		$index = array('_:A' => 
					array(
				'http://example.com/#knows'=>
								array(
									array('value'=>'_:B', 'type'=> 'bnode')
									)
							)
						);
		$rdfxml = $this->object->GraphIndex->to_rdfxml($index);
		$roundtripped_index =  $this->object->RDFXML->to_resources($rdfxml)->value();
		$this->assertEquals($index,$roundtripped_index);	
	}
	
	function test_GraphIndex_value_flatten()
	{
		$index = array('_:A' => 
					array(
				'http://example.com/#knows'=>
								array(
									array('value'=>'a', 'type'=> 'literal'),
									array('value'=>'b', 'type'=> 'literal'),
									array('value'=>'c', 'type'=> 'literal'),
									)
							)
						);
		
		$R = new GraphIndex($index);
		$actual = $R->value(array(), ':', true);
		$expected = array('_:A' => 
					array(
							'http://example.com/#knows'=>
								array(
									'a','b','c'
									)
							)
						);

			$this->assertEquals( $expected, $actual);
			
	}
	
	function test_facetsxml_to_array()
	{
		$sample = <<<_XML_
<?xml version="1.0"?>
<facet-results xmlns="http://schemas.talis.com/2007/facet-results#"><head><query>Keith Alexander</query><fields>name,nick</fields><top>10</top><output>xml</output></head><fields><field name="nick" /><field name="name"><term value="Keith Alexander" number="1" facet-uri="http://api.talis.com/stores/tomh-dev1/services/facet?fields=name%2Cnick&amp;top=10&amp;output=xml&amp;query=Keith+Alexander+AND+name%3A%22Keith+Alexander%22" search-uri="http://api.talis.com/stores/tomh-dev1/items?query=Keith+Alexander+AND+name%3A%22Keith+Alexander%22" /><term value="Keith J Alexander" number="1" facet-uri="http://api.talis.com/stores/tomh-dev1/services/facet?fields=name%2Cnick&amp;top=10&amp;output=xml&amp;query=Keith+Alexander+AND+name%3A%22Keith+J+Alexander%22" search-uri="http://api.talis.com/stores/tomh-dev1/items?query=Keith+Alexander+AND+name%3A%22Keith+J+Alexander%22" /></field></fields></facet-results>
_XML_;

$ex = array (
  'head' => 
  array (
    'query' => 'Keith Alexander',
    'fields' => 
    array (
      0 => 'name',
      1 => 'nick',
    ),
    'top' => '10',
    'output' => 'json',
  ),
  'fields' => 
  array (
    'nick' => 
    array (
    ),
    'name' => 
    array (
      0 => 
      array (
        'value' => 'Keith Alexander',
        'number' => '1',
        'facet_uri' => 'http://api.talis.com/stores/tomh-dev1/services/facet?fields=name%2Cnick&top=10&output=xml&query=Keith+Alexander+AND+name%3A%22Keith+Alexander%22',
        'search_uri' => 'http://api.talis.com/stores/tomh-dev1/items?query=Keith+Alexander+AND+name%3A%22Keith+Alexander%22',
      ),
      1 => 
      array (
        'value' => 'Keith J Alexander',
        'number' => '1',
        'facet_uri' => 'http://api.talis.com/stores/tomh-dev1/services/facet?fields=name%2Cnick&top=10&output=xml&query=Keith+Alexander+AND+name%3A%22Keith+J+Alexander%22',
        'search_uri' => 'http://api.talis.com/stores/tomh-dev1/items?query=Keith+Alexander+AND+name%3A%22Keith+J+Alexander%22',
      ),
    ),
  ),
);

		$actual = $this->object->FacetXML->to_array($sample)->value();
		$this->assertEquals($actual, $ex);

		
	}

	function test_resources_to_exhibit(){
		$ex = array (
		  'items' => 
		  array (
		    0 => 
		    array (
		      'id' => 'http://example.org/test/JohnSmith',
		      'label' => 'Label',
		      'type' => 'item',
		      'mbox' => 'mailto:john@example.org',
		    ),
		  ),
		  'properties' => 
		  array (
		    'label' => 
		    array (
		      'uri' => 'http://www.w3.org/2000/01/rdf-schema#label',
		      'label' => 'Label',
		      'valueType' => 'text',
		    ),
		    'mbox' => 
		    array (
		      'uri' => 'http://xmlns.com/foaf/0.1/mbox',
		      'label' => 'Mbox',
		      'valueType' => 'url',
		    ),
		  ),
		  'types' => 
		  array (
		    'item' => 
		    array (
		      'label' => 'Item',
		      'pluralLabel' => 'Items',
		    ),
		    '' => 
		    array (
		      'label' => '',
		    ),
		  ),
		);

		$actual = ($this->object->GraphIndex->to_exhibit($this->resources)->value());
		$this->assertEquals($actual, $ex);
	}
	
	function test_Resources_to_trix(){
		$actual = ($this->object->GraphIndex->to_trix($this->resources)->value());
		$ex = <<<_TRIX_
<?xml version="1.0"?>
<TriX xmlns="http://www.w3.org/2004/03/trix/trix-1/">
	<graph>

		<triple>
			<uri>http://example.org/test/JohnSmith</uri>
			<uri>http://xmlns.com/foaf/0.1/name</uri>
			<plainLiteral><![CDATA[John Smith]]></plainLiteral>	
		</triple>

		<triple>
			<uri>http://example.org/test/JohnSmith</uri>
			<uri>http://xmlns.com/foaf/0.1/mbox</uri>
			<uri>mailto:john@example.org</uri>	
		</triple>

		<triple>
			<uri>http://example.org/test/JohnSmith</uri>
			<uri>http://www.w3.org/2000/01/rdf-schema#label</uri>
			<plainLiteral xml:lang="en-gb"><![CDATA[Label]]></plainLiteral>	
		</triple>

	</graph>
</TriX>
_TRIX_;
		$this->assertEquals(preg_replace('/\s/','',trim($ex)), preg_replace('/\s/','',trim($actual)) );
	}

	function test_ResourceArray_merge()
	{
		
		$g1 = array(						//uri
			'#x' => array(						//prop
					'name' => array(				//obj
						array(
						'value' => 'Joe',
						'type' => 'literal',
							),
						),				//obj
				),					//prop
			'_:y' => array(
					'name' => array(array(
						'value' => 'Joan',
						'type' => 'literal',
						),),
				),
			
			);
			
			$g2 = array(
				'#x' => array(
						'knows' => array( array(
							'value' => '_:y',
							'type' => 'bnode',
							),
						),
					),
					
				'_:y' => array(
						'name' => array(
							array(
							'value' => 'Susan',
							'type' => 'literal',
							),
							),
					),

				);
			
			$g3 = array (
			  '#x' => 
			  array (
			    'name' => 
			    array (
			      0 => 
			      array (
			        'value' => 'Joe',
			        'type' => 'literal',
			      ),
			    ),
			    'knows' => 
			    array (
			      0 => 
			      array (
			        'value' => '_:y1',
			        'type' => 'bnode',
			      ),
			    ),
			  ),
			  '_:y' => 
			  array (
			    'name' => 
			    array (
			      0 => 
			      array (
			        'value' => 'Joan',
			        'type' => 'literal',
			      ),
			    ),
			  ),
			  '_:y1' => 
			  array (
			    'name' => 
			    array (
			      0 => 
			      array (
			        'value' => 'Susan',
			        'type' => 'literal',
			      ),
			    ),
			  ),
			);
		
		$Res = new GraphIndex($g1);
		$actual = $Res->import($g2);

		$this->assertEquals($g3, $actual);
		
	}

	function test_resourcesarray_reify(){

		$triple = array(
			'#foo' => array('#knows' => array(array('type'=>'uri','value' =>'#bar'))),
			);
			$RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
		$expected = array(
			'_:Statement1' => array(
				$RDF.'type' => array(
					array(
							'type' => 'uri',
							'value' => $RDF.'Statement',
						)
					),
				$RDF.'subject' => array(
						array(
								'type' => 'uri',
								'value' => '#foo',
							)
					),
				$RDF.'predicate' => array(
						array(
								'type' => 'uri',
								'value' => '#knows',
							)
					),
				$RDF.'object' => array(
						array(
								'type' => 'uri',
								'value' => '#bar',
							)
					),
				
				)
			);
		$actual = $this->object->GraphIndex->reify($triple)->value();
				$this->assertEquals($expected, $actual);
	}
	
	
	function test_resources_dereify(){
		
		$triple = array(
			'#foo' => array('#knows' => array(array('type'=>'uri','value' =>'#bar'))),
			);
		
		$expected = $triple;
		$actual = $this->object->GraphIndex->reify($triple)->dereify()->value();
						$this->assertEquals($expected, $actual);
	}
	
	function test_smushOwlSameAs()
	{
		$before = array(
			'#x' => array('#knows' => array(array('type'=>'uri','value' =>'#bar'))),
			'#y' => array('http://www.w3.org/2002/07/owl#sameAs' => array(array('type'=>'uri','value' =>'#x')),
											'#name' => array(array('type'=>'literal','value' =>'Fred')),
				),
			);
		$after = array(
			'#x' => array('#knows' => array(array('type'=>'uri','value' =>'#bar')), 
						  '#name' => array(array('type'=>'literal','value' =>'Fred')),
			),
			);
		$graph = new GraphIndex($before);
		$actual = $graph->smush($before)->value();
		$this->assertEquals($after, $actual);
	}
}

?>